package com.ztgf.user.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.models.bo.shop.ShopUserRelationBO;
import com.ztgf.shopping.models.dto.shop.ShopInfoDTO;
import com.ztgf.shopping.models.dto.shop.ShopUserRelationDTO;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.shopping.models.service.shop.IShopUserRelationService;
import com.ztgf.sms.models.bo.EmailSendBO;
import com.ztgf.sms.models.service.IEmailInfoService;
import com.ztgf.sys.models.bo.SysRoleMenuRelationBO;
import com.ztgf.sys.models.service.ISysMenuService;
import com.ztgf.sys.models.service.ISysRoleMenuRelationService;
import com.ztgf.user.common.config.YmlConfig;
import com.ztgf.user.mapper.UserInfoMapper;
import com.ztgf.user.models.bo.UserInfoSaveBO;
import com.ztgf.user.models.bo.UserInfoSubUpdateBO;
import com.ztgf.user.models.dto.UserInfoDTO;
import com.ztgf.user.models.dto.UserLoginDTO;
import com.ztgf.user.models.service.IUserInfoService;
import com.ztgf.user.models.service.IUserLoginService;
import com.ztgf.user.models.service.IUserRoleRelationService;
import com.ztgf.user.models.vo.UserInfoRoleRelationVO;
import com.ztgf.user.models.vo.UserInfoVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 用户基本信息控制器
 *
 * @author zhanghualang
 * @Date 2020-05-28 17:43:46
 */
@DubboService
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfoDTO> implements IUserInfoService{

    @Resource
    private YmlConfig ymlConfig;

    @DubboReference
    private IUserRoleRelationService iUserRoleRelationService;
    @DubboReference
    private IUserLoginService iUserLoginService;
    @DubboReference
    private IEmailInfoService iEmailInfoService;
    @DubboReference
    private ISysRoleMenuRelationService iSysRoleMenuRelationService;
    @DubboReference
    private ISysMenuService iSysMenuService;
    @DubboReference
    private IShopUserRelationService iShopUserRelationService;
    @DubboReference
    private IShopInfoService iShopInfoService;

    @Value(value = "${email_active.success_url}")
    private String email_active_success_url; //邮件激活成功地址

    @Value(value = "${email_active.fail_url}")
    private String email_active_fail_url; //邮件激活成功地址

    @Override
    public boolean add(UserInfoSaveBO saveBO) {
        UserInfoDTO dto = new UserInfoDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public boolean updateRole(UserInfoSubUpdateBO bo) {
        /**
         * 更新权限
         */
        List<Long> ids = iUserRoleRelationService.getRoleIdListByUserId(bo.getId());
        SysRoleMenuRelationBO sysRoleMenuRelationBO = new SysRoleMenuRelationBO();
        sysRoleMenuRelationBO.setRoleId(ids.get(0));
        sysRoleMenuRelationBO.setMenuIds(bo.getRoleIds());
        iSysRoleMenuRelationService.updateShopRoleMenu(sysRoleMenuRelationBO);

        UserInfoDTO dto = new UserInfoDTO();
        BeanUtils.copyProperties(bo, dto);
        dto.setId(bo.getId());
        return this.updateById(dto);
    }

    @Override
    public Page<UserInfoRoleRelationVO> queryUserPageList(ShopUserRelationBO bo) {
        Page<UserInfoRoleRelationVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<UserInfoRoleRelationVO> userInfoRoleRelationVOList = baseMapper.queryUserPageList(page, bo);
        for (UserInfoRoleRelationVO vo : userInfoRoleRelationVOList){
            Long roleId = iUserRoleRelationService.queryRoleListByUserId(vo.getUserId()).get(0).getRoleId();
            vo.setRoleId(roleId);
            vo.setSysMenuDTOS(iSysMenuService.selectListByRoleId(roleId));
        }
        return page.setRecords(userInfoRoleRelationVOList);
    }

    @Override
    public UserInfoVO getVo(Long id) {
        UserInfoDTO dto = this.selectById(id);
        UserInfoVO vo = new UserInfoVO();
        BeanUtils.copyProperties(dto,vo);

        //根据user_id获取userLogin 信息
        Wrapper<UserLoginDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", id);
        UserLoginDTO userLoginDTO = iUserLoginService.selectOne(wrapper);

        vo.setEmail(userLoginDTO.getEmail());
        vo.setMobile(userLoginDTO.getMobile());

        //根据user——id获取店铺信息
        Wrapper<ShopUserRelationDTO> suwrapper = new EntityWrapper<>();
        suwrapper.eq("user_id", id);
        ShopUserRelationDTO shopUserRelationDTO = iShopUserRelationService.selectOne(suwrapper);
        if (shopUserRelationDTO != null){
            ShopInfoDTO shopInfoDTO = iShopInfoService.selectById(shopUserRelationDTO.getShopId());
            vo.setIdCardType(shopInfoDTO.getIdCardType());
        }
        return vo;
    }

    @Override
    public HashMap<String, Object> activeEmail(Long userId, String captcha) {
        HashMap<String, Object> map = new HashMap<String, Object>();

        //根据user_id获取userLogin 信息
        Wrapper<UserLoginDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userId);
        UserLoginDTO userLoginDTO = iUserLoginService.selectOne(wrapper);
        if (userLoginDTO == null){
            map.put("tf", "0");
            map.put("url", email_active_fail_url);
            return map;
        }

        map.put("email", userLoginDTO.getEmail());
        map.put("userId", userId);

        UserInfoDTO dto = this.selectById(userId);
        if (dto == null || dto.getEmailActive().equals(1)){
            map.put("tf", "0");
            map.put("url", email_active_fail_url);
            return map;
        }

        //邮箱验证
        EmailSendBO emailSendBO = new EmailSendBO();
        emailSendBO.setEmail(userLoginDTO.getEmail());
        emailSendBO.setContext(captcha);
        emailSendBO.setType(2);

        boolean flag = iEmailInfoService.getCaptcha(emailSendBO);
        if (!flag){
            map.put("tf", "0");
            map.put("url", email_active_fail_url);
            return map;
        }

        UserInfoDTO userInfoDTO = new UserInfoDTO();
        userInfoDTO.setId(userId);
        userInfoDTO.setEmailActive(1);//邮箱激活 邮箱激活 0未激活，1已激活
        this.updateById(userInfoDTO);
        map.put("tf", "1");
        map.put("url", email_active_success_url);
        return map;
    }


    @Override
    public UserInfoVO getVoByName(String name) {
        Wrapper<UserInfoDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("name", name.trim());
        UserInfoDTO dto = this.selectOne(wrapper);
        UserInfoVO vo = new UserInfoVO();
        if (dto==null){
            return vo;
        }
        BeanUtils.copyProperties(dto,vo);

        //根据user_id获取userLogin 信息
        Wrapper<UserLoginDTO> lwrapper = new EntityWrapper<>();
        lwrapper.eq("user_id", dto.getId());
        UserLoginDTO userLoginDTO = iUserLoginService.selectOne(lwrapper);

        vo.setEmail(userLoginDTO.getEmail());
        vo.setMobile(userLoginDTO.getMobile());
        return vo;
    }

    @Override
    public UserInfoDTO selectByShopId(Long shopId) {
        UserInfoDTO userInfoDTO = baseMapper.selectByShopId(shopId);
        return userInfoDTO;
    }

}
